 package com.example.baiduai.exception;

 import com.example.baiduai.utils.JsonResult;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;

 @Slf4j
 @ResponseBody
 @ControllerAdvice
 public class GlobalExceptionHandler {

     //捕捉普通的异常
     @ExceptionHandler(value = Exception.class)
     public JsonResult defaultErrorHandler(Exception e) {
         log.error("系统异常：",e);
         e.printStackTrace();
         return JsonResult.fail(e.getMessage());

     }

     //捕捉自己定义的异常（BusinessException.class）
     @ExceptionHandler(value = PlatformException.class)
     public JsonResult businessErrorHandler(PlatformException e) {
         log.error("业务异常", e);
         e.printStackTrace();
         //自己定义的运行时异常
         JsonResult result = new JsonResult();
         result.setCode(e.getCode());
         result.setMsg(e.getMsg());
         return result;
     }

     //捕捉自己定义的异常（BusinessException.class）
     @ExceptionHandler(value = NullPointerException.class)
     public JsonResult nullPointerExceptionHandler(NullPointerException e) {
         log.error("未知空异常", e);
         e.printStackTrace();
         //自己定义的运行时异常
         return JsonResult.fail("未知空异常!");
     }

 }
